/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jigdo.data.template.section;

import jigdo.data.template.JigdoTemplate;
import jigdo.data.template.section.DESCEntries.TemplateDESCEntry;
import jigdo.data.template.section.DESCEntries.*;

/**
 *
 * @author John R Sohn
 */
public class TemplateDESCSection extends TemplateSection {
    
    
    private int countFileEntries=0;
    private int countRawDataEntries=0;
    private int countIsoImageEntries=0;
    
    public TemplateDESCImageInfoEntry ImageInfo;
    
    public class FileSearchEntry {

        public String MD5;
        public TemplateDESCFileInfoEntry Parent;

        public FileSearchEntry(TemplateDESCFileInfoEntry Entry) {
            Parent = Entry;
            MD5 = Entry.FileMD5Base64;
        }
    }

    public java.util.ArrayList<FileSearchEntry> GetSearch() {
        java.util.ArrayList<FileSearchEntry> res = new java.util.ArrayList<FileSearchEntry>();


        for (int x = 0; x < Entries.size(); x++) {
            if (TemplateDESCFileInfoEntry.class.isInstance(Entries.get(x))) {
                res.add(new FileSearchEntry((TemplateDESCFileInfoEntry) Entries.get(x)));
            }
        }

        return res;
    }

    public DESCDataIterator GetIterator()
    {
        return new DESCDataIterator(this);
    }
    
    public TemplateDESCSection(JigdoTemplate parent) {
        super(parent);
        Entries = new java.util.ArrayList<TemplateDESCEntry>();
        this.SectionType = jigdo.data.template.TemplateType.DESC;
    }
    public java.util.ArrayList<TemplateDESCEntry> Entries;

    @Override
    public void Interpret() throws jigdo.JigdoException {
        int index = 0;
        long fileoffset = 0;

        while (index < this.Length - 16) {
            int type = jigdo.utils.ByteUtils.GetUnsigned(this.DataChunk[index]);
            index++;

            TemplateDESCEntry section;

            switch (type) {
                case 5:
                    //"Information about the image file"

                    section = new TemplateDESCImageInfoEntry(this, this.Entries.size(), fileoffset);
                    ImageInfo = (TemplateDESCImageInfoEntry)section;
                    this.countIsoImageEntries++;
                    break;

                case 2:
                    //"Unmatched data, contained in 'Raw data'"

                    section = new TemplateDESCRawDataEntry(this, this.Entries.size(), fileoffset);
                    this.countRawDataEntries++;
                    break;

                case 6:
                    //"Information about matched file"
                    section = new TemplateDESCFileInfoEntry(this, this.Entries.size(), fileoffset);
                    this.countFileEntries++;
                    break;

                default:

                    throw new jigdo.JigdoException("Unrecognized type value encountered while parsing DESC Section");


            }

            index = section.ReadEntry(DataChunk, index);

            fileoffset += section.DataSize;

            this.Entries.add(section);

        }

        if (!TemplateSection.RetainDataChunk) {
            this.DataChunk = null;
        }
    }
    
    public int getNumberFileEntries()
    {
        return this.countFileEntries;
    }
    
    public int getNumberDataEntries()
    {
        return this.countRawDataEntries;
    }
}
